
  /*找医生*/

$(function() {

	////初始化详情   初始化医生
	$(window).on("load", queryDetail);
	// 收藏医生
	$(".concern").on('click',doctorAttent);
	//提交确认
	$("#confrimBtn").on("click", confrimBtn);
	
	//更多评价
	$(".more-comment").on("click",queryMoreComment);

	//详情信息
	function queryDetail() {
		var doctorId = utilModule.getQueryString("doctorid");
		if(utilModule.checkUndefined(doctorId)==""){
		  return false;
		}
		var params = {
			doctorId: doctorId	
		};
		doctorModule.queryDetail(params, function(callBack) {
			var data = callBack 
			if(data != null) {
				var disList = data.diseaseList;
				if(disList != null) {
					initDieasList(disList);
				}
				initDetail(data.doctor);
			}
		});
		var param = {
			patientid: USERID,
			doctorid: doctorId	
		}
		doctorModule.doctorAttentionList(param,function(callBack) {
			  var lists = callBack;
			  for (var skey in lists) {
			  	if(parseInt(lists[skey])==param.doctorid){
			  		$(".concern .add-doctor").hide();
						$(".concern .remove-doctor").show();
			  	};
			  }
		});
	
	}
	//  收藏医生 
	function doctorAttent(){
			var doctorId = utilModule.getQueryString("doctorid");
			if(utilModule.checkUndefined(doctorId)==""){
			   	return false;
			}
			var param = {
					patientid: USERID,
					doctorid: doctorId	
			};
			doctorModule.doctorAttention(param,function(callBack){
				  var result = callBack;
				  if(result.msg=='取消关注成功'){
							$(".concern .add-doctor").show();
							$(".concern .remove-doctor").hide();
					}else{
							$(".concern .add-doctor").hide();
							$(".concern .remove-doctor").show();
					}
			});
	}
	
	//初始化症状信息,至多选择6个标签
	function initDieasList(data) {
		var diseaseDiv = $(".doctor2-expertise");
		for(var n in data) { 
			if(n>5){return ;}
			var p = document.createElement("li");
			p.innerHTML = data[n].disname;
			diseaseDiv.append(p);
		}
	}

	//详情展示
	function initDetail(doctor) {
			
 	  $(".picture").find("img").attr("src",doctor.avatar);
 	  
	  $("#hospitalName").text(doctor.hospitalName==null?"默认医院":doctor.hospitalName);
 	  $("#uName").text(doctor.realname); 
 	  $(".uName").text(doctor.realname); 
 	  $("#deptName").html(doctor.deptName==null?"精神科":doctor.deptName); 
 	  $("#xlys").html(doctor.doctortitle);
 	  $(".xlys").html(doctor.doctortitle);
 	  $("#skilled").text(doctor.skilful);
 	  $("#pCount").text(doctor.concernnumber);
    $("#satisfaction").html(doctor.commentscore==" "?(doctor.commentscore)*20+"%":100+'%');
    $("#serviceTimes").html(doctor.servicetimes);
   
    if(doctor.introduce){
      	   $("#doctorDes").html(doctor.introduce); 
 	  }else{
 	       $("#doctorDes").html('馨语医疗只为关注您的健康!'); 
 	  }
      $("#doctorid").val(doctor.id);
  
      //初始化订单类型,1:图文 2:电话
      var orderType=1;
      
      //图文咨询价格
      var imisopen=doctor.imisopen==null?0:doctor.imisopen;//是否开启图文咨询,0，关闭 1，开启
      var imprice=doctor.imprice==null?0.00:doctor.imprice.toFixed(2);
       //未开启电话咨询
      if(imisopen==0 || imprice==0.00){
      	 $("#imisopen").val(0);
      	 $(".doctor_icon1 a").css('color',"#999");
      	 $(".doctor_icon1 span").css('color',"#999");
      	 $(".doctor_icon1").find("span").html("未开通");
      	 $(".images-text").hide();
      	 $(".brompts").hide();
      	 orderType=2;
      }else{
	      $(".textshowPirce").html(imprice);
	      $("#payAmount").val(imprice);
	      $("#textOrderDetail").val(doctor.realname+"-图文咨询:"+doctor.hospitalName);
	  }
      
      //电话咨询价格 TODO://
      var phoneisopen=doctor.phoneisopen==null?0:doctor.phoneisopen;//是否开启电话咨询,0，关闭 1，开启
      var phoneprice=doctor.phoneprice==null?0.00:doctor.phoneprice;//电话咨询价格
     
      //未开启电话咨询
      if(phoneisopen==0 || phoneprice==0.00 ||phoneprice==0){
      	 $("#phoneisopen").val(0);
      	  $(".doctor_icon2 a").css('color',"#999");
		 		 $(".doctor_icon2 span").css('color',"#999");
      	 $(".doctor_icon2").find("span").html("未开通");
      	 $(".tel-text").hide();
      }else{
      	 $(".phoneshowPirce").html(phoneprice.toFixed(2));
     	 		$("#phonePayAmount").val(phoneprice);
      	 $("#phoneOrderDetail").val(doctor.realname+"-电话咨询:"+doctor.hospitalName);
      }
      
      $("#orderType").val(orderType);
      
	}

	//提交咨询
	
	function confrimBtn(){
		var orderType=$("#orderType").val();
		console.log(orderType);
		if(orderType==1){
			textOrder();
		}else if(orderType==2){
			phoneOrder();
		}
	}
	
	//电话咨询
	function phoneOrder(){

		var consultOrder={};
		var doctorid=$("#doctorid").val();
		var serviceDate=$("#serviceDate").val();
		var serviceTime=$("#serviceTime").val();
	
		if(serviceDate==null || serviceDate==""){
			mui.alert("请选择预约日期");
			return false;
		}
		if(serviceTime==null || serviceTime==""){
			mui.alert("请选择预约时间");
			return false;
		}
			
		consultOrder.doctorid=doctorid;
		consultOrder.payAmount=$("#phonePayAmount").val();
		consultOrder.orderDetail=$("#phoneOrderDetail").val();
		consultOrder.serviceTime=serviceDate+" "+serviceTime;//服务时间
		var endTime =serviceTime.substring(serviceTime.length-5,serviceTime.length);
		consultOrder.expireTime=serviceDate+" "+ endTime;//截止时间
		consultOrder.orderType=2;
	 	console.log(consultOrder);
	    var orderKey="doctor_index";
	    //var backUrl =window.location.href;
	    sessionStorage.setItem(orderKey,JSON.stringify(consultOrder));
	   
	   //提问
	   location.replace("ask-doctor.html?doctorid="+doctorid+"&orderKey=2");
		
		
	}

	//图文咨询:
	//备注：如果与该医生有未完成订单，则直接跳转到IM界面；否则跳转提问界面
	function textOrder(){
		var consultOrder={};
		var doctorid=$("#doctorid").val();
		consultOrder.doctorid=doctorid;
		consultOrder.payAmount=$("#payAmount").val();
		consultOrder.orderDetail=$("#textOrderDetail").val();
		consultOrder.orderType=1;
		//查询未完成订单
		
		var resultObject=queryUnfinishedOrder(doctorid);
		if(resultObject!=null){
			checkUnfinishedOrder(USERID,doctorid);
		}else{
			var orderKey="doctor_index";
		  sessionStorage.setItem(orderKey,JSON.stringify(consultOrder));
		  //提问
		  location.replace("ask-doctor.html?doctorid="+doctorid);
		  
		}	
	}
	
	//查询未支付订单
	function queryUnfinishedOrder($doctorid){
		var orderVo=null;
		var params={
			userid:USERID,
			doctorid:$doctorid
		}
		orderPayModule.queryUnfinishedOrder(params,function(callBack) {
			if(callBack != null) {
				if(utilModule.checkUndefined(callBack.id)!=""){
				   orderVo=callBack;
				}
			}
		});
		return orderVo;
	}
	
	//跳转聊天im界面
	function invokeIm(orderVo) {
		var orderid = orderVo.id;
		var payStatus=parseFloat(orderVo.payamount)==0.00?0:1;//免費咨詢	
		var params = {
			"doctorId": orderVo.doctorid
		};
		
		doctorModule.queryDetail(params, function(callBack) {
			var result = callBack;
			if(result != null) { 
				var doctorInfo = result.doctor;
				//doctor_user_20
				var doctorObject = {};
				doctorObject.doctorid = doctorInfo.id;
				var usernameIM = DOCTOR_IM_PREFFIX+doctorInfo.id;
				doctorObject.usernameIM = usernameIM;
				doctorObject.avatar = doctorInfo.avatar;
				doctorObject.realname = doctorInfo.realname;
				doctorObject.deptName=doctorInfo.deptName;
				utilModule.setStorage(usernameIM, JSON.stringify(doctorObject));
				mui.toast("正在跳转咨询界面....");
				console.log("医生信息:" + utilModule.getStorage(usernameIM));
				//支付成功跳转im聊天界面   图文：2
				location.replace("../inquiry/inquiry-service.html?usernameIM=" + usernameIM+"&orderid="+orderid+"&payStatus=2&doctorid="+doctorInfo.id);
			}
		});
	}

		//查看更多评价
		function queryMoreComment(){
			var doctorId = utilModule.getQueryString("doctorid");
			if(utilModule.checkUndefined(doctorId)==""){
			   return false;
			}
			location.href="more-comment.html?doctorid="+doctorId;
		}
	
		//更多医生简介
		function queryMoreAbstract (){
			var doctorId = utilModule.getQueryString("doctorid");
			if(utilModule.checkUndefined(doctorId)==""){
			   return false;
			}
			location.href="doctor-abstract.html?doctorid="+doctorId;
		}
	
	
});